﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
using System.Data;
using System.Data.SqlClient;
namespace Webcar.Controllers
{
    public partial class TableController : BaseControler
    {
        public ActionResult GpsInstallList()
        {
            var list = from p in CurrentUser.UserGrantedDept.Where(a=>a.super_dept==null&& a.use_flag == "T")
                       join p2 in WebcarDataProvider.DB.gps_tj
                       on p.deptcode equals p2.dept_code
                       into result
                       from bb in result.DefaultIfEmpty()
                       select new {
                           dept_code=p.deptcode,
                           dept_name=p.deptname,
                           cjdw=bb.cjdw,
                           cjdw_lxr = bb.cjdw_lxr,
                           cjdw_dh=bb.cjdw_dh,
                           gps_num=bb.gps_num,
                           tbr=bb.tbr,
                           tbr_lxdh=bb.tbr_lxdh,
                           tbrq=bb.tbrq,
                           note=bb.note
                       };
            return View(model:list.ToArray());
        }
        public ActionResult GpsInstallEdit(string id)
        {
            gps_tj model = null;
            if (id.IsNotNull())
            {
                model = WebcarDataProvider.DB.gps_tj.Find(id);
            }
            if (model == null)
            {
                model = new gps_tj();
                model.tbrq = DateTime.Now;
                model.dept_code = id;
            }
            var dept = WebcarDataProvider.DB.sys_dept.Find(id);
            if (dept != null)
            {
                ViewBag.DeptName = dept.deptname;
            }
            return PartialView(model: model);
        }
        public ActionResult GpsInstallEditSave(gps_tj model)
        {
            string error = "";
            if (CheckGpsInstallModel(model, ref error) == false)
            {
                return Content(error);
            }
            using (var DB = new WebcarContext())
            {
                gps_tj item = DB.gps_tj.Find(model.dept_code);
                if (item == null)
                {
                    DB.gps_tj.Add(model);
                }
                else
                {
                    model.CopyPropertyTo(item);
                }
                DB.SaveChanges();
            }
            return SuccessReload();
        }

        bool CheckGpsInstallModel(gps_tj model, ref string error)
        {
            if (model.dept_code.IsNull())
            {
                error = "请输入单位。";
                return false;
            }
            if (model.cjdw.IsNull())
            {
                error = "请输入承建单位。";
                return false;
            }
            if (model.cjdw_lxr.IsNull())
            {
                error = "请输入承建单位联系人。";
                return false;
            }

            return true;
        }
    }
}